/*

Copyright (C) 2007 Thomas Geiger (tom.geiger@gmail.com)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

#include "URL.h"

namespace globus_common {

// protected constructor
URL::URL() {}

URL::URL(globus_url_t* url, char* urlPath) {
	globus_url = *url;	
	urlString = urlPath;
}

// destructor
URL::~URL() {
	globus_url_destroy(&globus_url);
}

// factory methods
URL* URL::Create(char* urlPath) 
{
	globus_url_t url;
    int res;
	// parse url to globus_url_t struct
	res = globus_url_parse_rfc1738(urlPath, &url); 
	if (res != GLOBUS_SUCCESS) {
         cerr << "Can not parse url " << urlPath << ", " << res << endl;
	     return NULL;
	}
	
	return new URL(&url, urlPath);
}

URL* URL::Create(string& urlPath) {
	return Create( const_cast<char*>( urlPath.c_str() ) );	
}

string URL::getScheme() {
	return string(globus_url.scheme);	
}

// returns url
string URL::getURL() {
	return string(urlString);
}

globus_url_scheme_t URL::getType() {
	return globus_url.scheme_type;
}

string URL::getUser() {
	if (globus_url.user == NULL) {
		return EMPTY;	
	}
	return string(globus_url.user);
}
		
string URL::getPassword() {
	if (globus_url.password == NULL) {
		return EMPTY;	
	}
	return string(globus_url.password);
}
	
string URL::getHost() {
	return string(globus_url.host);
}
		
int URL::getPort() {
	return globus_url.port;
}

/*
int main() {

	GlobusURL* url = GlobusURL::Create("ftp://grid.labs.fhv.at/fhv-users/tge1468/sourcefile");
	if (url == NULL) {
		cout << "houston we have a problem." << endl;
	} else {
		cout << "yay" << endl;
		cout << url->getScheme() << ", " << url->getURL() << ", " << url->getType() << endl;
		if (url->getType() == GLOBUS_URL_SCHEME_HTTP) {
			cout << "HTTP" << endl;	
		}
		cout << url->getHost() << ", " << url->getPort() << endl;	
		if (url->getPassword() != "") {
			cout << url->getPassword() << endl;	
		}
		if (url->getUser() != "") {
			cout << url->getUser() << endl;	
		}

	}
}
*/

}
